import { Component, ViewChild } from '@angular/core';
// import { EditWuliuComponent } from './edit-wuliu/edit-wuliu.component';
// import { AddWuliuComponent } from './add-wuliu/add-wuliu.component';
import { DataTableComponent, DialogService, TableWidthConfig } from '@devui';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
// import { WuliuService } from 'src/app/@core/services/wuliu/wuliu.service';

@Component({
  selector: 'app-wuliu',
  templateUrl: './wuliu.component.html',
  styleUrls: ['./wuliu.component.scss']
})
export class WuliuComponent {
  tableData:any[]=[]
    tableheight = 'calc(100vh - 380px)';
    allCheckedStatus = false;
    pager = {
      total: 20,
      pageIndex: 1,
      pageSize: 20
    };
    checkedIds = [];
    pageSizeOptions = [10, 20, 30, 50, 100];
    msgs: { severity: any; summary: any; content: any; }[];
    config = {
      id: 'Add-role',
      maxHeight: '400px',
      width: '400px',
      backdropCloseable: true,
    }//弹窗配置
    filterValue:string = '';
    TreeData:any = [];
    @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
    state: any;
    constructor(private dialogService: DialogService,private SharedMethodService:SharedMethodService){}
    ngOnInit(): void {
      // 模拟物流数据
      this.tableData = [
        {
          logisticsId: 1,
          logisticsCode: 'WL001',
          companyName: '顺丰速运',
          contactPhone: '400-111-1111',
          contactPerson: '张三',
          address: '深圳市南山区科技园',
          status: '启用',
          isEnabled: true,
          createTime: new Date('2024-01-15 10:30:00')
        },
        {
          logisticsId: 2,
          logisticsCode: 'WL002',
          companyName: '圆通快递',
          contactPhone: '400-222-2222',
          contactPerson: '李四',
          address: '上海市浦东新区张江高科',
          status: '启用',
          isEnabled: true,
          createTime: new Date('2024-02-20 14:15:00')
        },
        {
          logisticsId: 3,
          logisticsCode: 'WL003',
          companyName: '中通快递',
          contactPhone: '400-333-3333',
          contactPerson: '王五',
          address: '杭州市滨江区网商路',
          status: '禁用',
          isEnabled: false,
          createTime: new Date('2024-03-10 09:45:00')
        },
        {
          logisticsId: 4,
          logisticsCode: 'WL004',
          companyName: '申通快递',
          contactPhone: '400-444-4444',
          contactPerson: '赵六',
          address: '北京市朝阳区建国路',
          status: '启用',
          isEnabled: true,
          createTime: new Date('2024-04-05 16:20:00')
        },
        {
          logisticsId: 5,
          logisticsCode: 'WL005',
          companyName: '德邦物流',
          contactPhone: '400-555-5555',
          contactPerson: '孙七',
          address: '广州市天河区珠江新城',
          status: '启用',
          isEnabled: true,
          createTime: new Date('2024-05-12 11:30:00')
        }
      ];
      this.pager.total = this.tableData.length;
    }
    dataTableOptions = {
      columns: [
        {
          field: 'logisticsCode',
          header: '物流编码',
          fieldType: 'text',
        },
        {
          field: 'companyName',
          header: '物流公司名称',
          fieldType: 'text',
        },
        {
          field: 'contactPhone',
          header: '联系电话',
          fieldType: 'text'
        },
        {
          field: 'contactPerson',
          header: '联系人',
          fieldType: 'text'
        },
        {
          field: 'address',
          header: '地址',
          fieldType: 'text'
        },
        {
          field: 'status',
          header: '状态',
          fieldType: 'text'
        },
        {
          field: 'createTime',
          header: '创建时间',
          fieldType: 'text'
        },

      ]
    };//表格数据
    tableWidthConfig: TableWidthConfig[] = [
      {
        field: 'checkbox',
        width: '80px'
      },
      {
        field:'name',
        width:'150px'
      },
      {
        field:'name',
        width:'150px'
      },
      {
        field:'name',
        width:'150px'
      },
      {
        field:'name',
        width:'150px'
      },
      {
        field:'name',
        width:'150px'
      },
      {
        field:'name',
        width:'150px'
      },
      {
        field:'name',
        width:'150px'
      },
    ];//表格宽度配置
    pageIndexChange($event: any) {
      this.pager.pageIndex=$event;
      // TODO: 实现物流数据分页查询
      console.log('切换页码到:', $event);
    }//切换页码
    pageSizeChange($event: any){
      // TODO: 实现物流数据每页数量变更
      console.log('每页显示数量变更为:', $event);
    }//改变每页显示数量
    DeleteRole(rowitem:any,dialogtype?:string){
      const results = this.dialogService.open({
        ...this.config,
        title: '删除物流公司',
        content: '确认删除该物流公司？',
        dialogtype: dialogtype,
        onClose: () => {
        },
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',
            handler: ($event: Event) => {
              // TODO: 实现删除物流公司
              console.log('删除物流公司:', rowitem);
              this.showToast('success','成功','物流公司删除成功');
              results.modalInstance.hide();
            },
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          },
        ],
      });

    }//删除
    enable(){
      // TODO: 实现批量启用物流公司
      console.log('启用物流公司:', this.checkedIds);
      this.showToast('success','成功','物流公司启用成功');
    }//启用
    disable(){
      // TODO: 实现批量禁用物流公司
      console.log('禁用物流公司:', this.checkedIds);
      this.showToast('success','成功','物流公司禁用成功');
    }//禁用
    showToast(type: any,summary:any,content:any) {
      switch (type) {
      default:
        this.msgs = [{ severity: type, summary: summary, content: content }];
      }
    }//提示框
    Creat(dialogtype?:string){
      let data = {
        display: false,
      };
      const results = this.dialogService.open({
        ...this.config,
        data:data,
        title: '添加物流公司',
        content: '添加物流公司功能待实现', // 暂时用字符串替代组件
        dialogtype: dialogtype,
        onClose: () => {
        },
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',  
            handler: ($event: Event) => {
              this.showToast('success','成功','物流公司添加成功');
              results.modalInstance.hide();
            },
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          }
        ],
      });
    }//创建物流公司
    EditRole(rowItem:any,dialogtype?:string){
      let data = {
        rowItem:rowItem,
        display: false,
      };
      const results = this.dialogService.open({
        ...this.config,
        data:data,
        title: '更新物流公司',
        content: '更新物流公司功能待实现', // 暂时用字符串替代组件
        dialogtype: dialogtype,
        onClose: () => {
        },
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',
            handler: ($event: Event) => {
              // TODO: 实现更新物流公司
              console.log('更新物流公司:', rowItem);
              this.showToast('success','成功','物流公司更新成功');
              results.modalInstance.hide();
            },
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          },
        ],
      });

    }//更新物流公司
    filter(){
      // TODO: 实现物流公司搜索
      console.log('搜索物流公司:', this.filterValue);
    }//搜索
    reset(){
      this.filterValue = '';
      // TODO: 重置物流公司列表
      console.log('重置搜索');
    }//重置
    onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }){
      this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem,this.datatable)

    }
    getCheckedRows() {
      this.SharedMethodService.getCheckedRows(this.datatable,'logisticsId').subscribe((data:any)=>{
        this.checkedIds=data;
      })
    }//获取选中行
    cleared(){
      this.SharedMethodService.cleared(this.checkedIds,this.tableData,this.datatable).subscribe((data:any)=>{
        this.checkedIds=data;

      })
    }//清空选中行
}
